package org.maxgroundstation.groundstation.comm.service.alive;

import org.maxgroundstation.groundstation.comm.service.Service;
import org.maxgroundstation.groundstation.comm.service.ServiceCommunication;
import org.maxgroundstation.groundstation.comm.service.ServiceDataFrame;
import org.maxgroundstation.groundstation.comm.service.ServiceUtil;

/**
 * The I'm alive service.
 * (Note: this service is no longer being used with the MAX kernel.)
 * @author Scott Davis
 * 
 * Copyright (C) 2010  Scott Davis
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class ImAliveService extends Service implements Runnable {

	// Static members
	public static final String SERVICE_ID = ServiceUtil.getServiceID("__alive");
	public static final String TYPE = "alive";
	
	// Length of alive thread sleep time (milliseconds).
    private static final long SLEEP_TIME = 1000L;
	
	// Data members
    private Thread aliveThread;
    private boolean run;
	
	/**
	 * Constructor
	 * @param serviceCommunication the service communication.
	 */
	public ImAliveService(ServiceCommunication serviceCommunication) {
		// Use Service constructor.
		super(serviceCommunication);
	}
	
	@Override
	public String getID() {
		return SERVICE_ID;
	}
	
	@Override
	public boolean isSubscribable() {
		return false;
	}
	
	@Override
	public void initialize() {
		// Start alive thread.
		start();
	}
	
	@Override
	public void closeDown() {
	    super.closeDown();
	    
		// Stop alive thread.
		run = false;
	}

	@Override
	public void handleResponse(ServiceDataFrame dataFrame) {
		// Do nothing.
	}

	@Override
	public boolean isDefaultService() {
		return false;
	}
	
	/**
     * Starts the alive thread.
     */
    private void start() {
        if ((aliveThread == null) || !run) { 
            aliveThread = new Thread(this, "I'm alive thread");
            aliveThread.start();
        }
    }
    
    @Override
    public void run() {
    	run = true;
        while (run) {
            try {
                Thread.sleep(SLEEP_TIME);
            } 
            catch (InterruptedException e) {}
        	
        	try {
        		//System.out.println("Sending I'm alive service post");
        		getServiceCommunication().sendServicePost(SERVICE_ID, new byte[0]);
        	}
        	catch (Exception e) {
        		e.printStackTrace(System.err);
        	}
        }
    }

    @Override
    public String getType() {
        return TYPE;
    }
}